package com.example.controller;


import com.aiseed.dao.CourseInfoDTO;
import com.aiseed.mapper.CourseInfoMapper;
import com.aiseed.service.CourseDescriptionService;
import com.aiseed.service.CourseInfoService;
import com.aiseed.utils.Result;
import com.aiseed.utils.ResultEnum;
import com.aiseed.utils.ResultUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/*
*<p>
*课程 前端控制器
*</p>
*/

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/course")
public class CourseInfoController {
 @Autowired
 private CourseInfoMapper CourseInfoMapper;
 @Autowired
 private CourseInfoService courseInfoService;
 @Autowired
 private CourseDescriptionService CourseDescriptionService;

/**
  * 根据课程id查询课程
  * @param id
  * @return
  */

 @ApiOperation("根据课程id查询课程信息")
 @GetMapping("{id}")
 public Result getCourseById(@ApiParam(name = "id", value = "课程id", required = true)
                               @PathVariable String  id){
  log.info("【查询课程信息】 查询课程id为"+ id +"的信息");
  CourseInfoDTO courseInfoDTO = CourseInfoService.findCourseById(id);

  return ResultUtils.success(courseInfoDTO);
 }

 /**
  * 2. 根据id删除课程
  * @param id
  * @return
  */
    @ApiOperation("根据id删除课程")
    @DeleteMapping("{id}")
    public Result deleteCourseId(@PathVariable String id) {

        log.info("【删除课程】 删除id为："+id+"的课程");
        if( CourseInfoService.removeCourseId(id) ){
            return ResultUtils.success();
        }else{
            return ResultUtils.error( ResultEnum.ERROR);

        }
    }

/**
  * 3.保存课程信息
  * @param courseInfoDTO
  * @return
  */

 @ApiOperation("保存课程信息")
 @PostMapping
 public Result saveCourse(@ApiParam(name = "CourseInfoDTO", value = "课程信息", required = false)
                            @RequestBody( required = false) CourseInfoDTO courseInfoDTO ) {

  String courseId = CourseInfoService.saveCourseInfo(courseInfoDTO);
  HashMap<String, String> map = new HashMap<>();
  map.put("courseId", courseId);
  return ResultUtils.success(map);
 }


}
